package com.wuyan.locationdog.ui.my

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.DeviceShareCustomBean
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.widgets.dialog.CommonDialog
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.SharePersonListAdapter
import com.wuyan.locationdog.databinding.MySharePersonListActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class MySharePersonListActivity
    : BaseBindingActivity<MySharePersonListActivityBinding>() {
    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { SharePersonListAdapter(R.layout.item_share_person_list, null) }

    override fun initData() {
        setTitleText("设备共享管理")


        binding.apply {
            deviceName.text = DeviceHelp.getDeviceName(DeviceHelp.deviceShareBeanContent)
            imei.text = "设备ID：" + DeviceHelp.deviceShareBeanContent.imei

            shareCount.text = "已共享给"+DeviceHelp.deviceShareBeanContent.shareCount+"位好友"
            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("还未共享给任何好友"))

                    addChildClickViewIds(R.id.btnDelete)
                    setOnItemChildClickListener { adapter, view, position ->
                        CommonDialog(mActivity, title = "确定取消 [${data[position].nickName}] 的使用权限？", "") {
                            val map = HashMap<String, Any>()
                            map["shareId"] = data[position].shareId
                            mViewModel.shareCancel(map)

                        }.show()
                    }

                    addChildLongClickViewIds(R.id.itemLayout)
                    setOnItemChildLongClickListener { adapter, view, position ->
                        CommonDialog(mActivity, title = "确定取消 [${data[position].nickName}] 的使用权限？", "") {
                            val map = HashMap<String, Any>()
                            map["shareId"] = data[position].shareId
                            mViewModel.shareCancel(map)

                        }.show()
                        true
                    }
                }
            }
            btnSure.setOnClickListener {
                DeviceHelp.deviceShareBeanContent.apply {
                    DeviceHelp.deviceShareList =
                        mutableListOf(DeviceShareCustomBean(dogImg, deviceId, DeviceHelp.getDeviceName(this), imei))
                }
                ActivityUtils.startActivity(MyShareAddPersonListActivity::class.java)
            }

        }


    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        map["pageNum"] = 1
        map["pageSize"] = 999
        map["deviceId"] = DeviceHelp.deviceShareBeanContent.deviceId
        mViewModel.sharePersonList(map)
    }
    override fun observe() {
        super.observe()
        mViewModel.anyBean.observe(this) {
           loadNetData()
        }
        mViewModel.deviceShareBean.observe(this) {
            mAdapter.setNewInstance(it.content.toMutableList())
        }
    }


}